<template>
  <div>
    <div class="filter-container">
      <el-checkbox-group v-model="checkboxVal">
        <el-checkbox label="date">日期</el-checkbox>
        <el-checkbox label="name">名字</el-checkbox>
        <el-checkbox label="address">地址</el-checkbox>
      </el-checkbox-group>
    </div>
    <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange"
      :default-sort="{prop: 'id', order: 'aescending'}" :key='key' border fit highlight-current-row stripe>
      <el-table-column fixed type="selection" width="55"></el-table-column>
      <el-table-column sortable prop="id" label="id" width="180"></el-table-column>
      <!-- <el-table-column prop="date" label="日期" sortable width="180" :filters="[{text: '2016-05-01', value: '2016-05-01'}, {text: '2016-05-02', value: '2016-05-02'}, {text: '2016-05-03', value: '2016-05-03'}, {text: '2016-05-04', value: '2016-05-04'}]"
        :filter-method="filterHandler"></el-table-column> -->

      <!-- <el-table-column label="日期" sortable width="120">
        <template slot-scope="scope">{{ scope.row.date }}</template>
      </el-table-column> -->

      <!-- <el-table-column prop="name" label="姓名" sortable width="120"></el-table-column>
      <el-table-column prop="address" label="地址" sortable show-overflow-tooltip :formatter="formatter"></el-table-column> -->

      <el-table-column sortable :key='item' v-for='item in formThead' :label="getTableHeaderLabel(item)" :prop="item">
        <template slot-scope="scope">
          {{scope.row[item]}}
        </template>
      </el-table-column>

    </el-table>
    <div style="margin-top: 20px">
      <el-button @click="toggleSelection([tableData[1], tableData[2]])">切换第二、第三行的选中状态</el-button>
      <el-button @click="toggleSelection()">取消选择</el-button>
    </div>
  </div>
</template>

<script>
  const defaultFormThead = ['date']
  export default {
    name:'test-table-filter',
    data() {
      return {
        tableData: [{
          id: '1',
          date: '2016-05-03',
          name: '王小虎1',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          id: '2',
          date: '2016-05-02',
          name: '王小虎2',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          id: '3',
          date: '2016-05-04',
          name: '王小虎3',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          id: '4',
          date: '2016-05-01',
          name: '王小虎4',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          id: '5',
          date: '2016-05-08',
          name: '王小虎5',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          id: '6',
          date: '2016-05-06',
          name: '王小虎6',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          id: '7',
          date: '2016-05-07',
          name: '王小虎7',
          address: '上海市普陀区金沙江路 1518 弄'
        }],
        multipleSelection: [],
        key: 1, // table key
        formTheadOptions: ['date', 'name', 'address'],
        checkboxVal: defaultFormThead, // checkboxVal
        formThead: defaultFormThead, // 默认表头 Default header
        tableHeadeList:{
            'id':'编号',
            'name':'姓名',
            'date':'日期',
            'address':'地址',
        }
      }
    },
    watch: {
      checkboxVal(valArr) {
        this.formThead = this.formTheadOptions.filter(i => valArr.indexOf(i) >= 0)
        this.key = this.key + 1 // 为了保证table 每次都会重渲 In order to ensure the table will be re-rendered each time
      }
    },
    methods: {
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      formatter(row, column) {
        return row.address;
      },
      filterHandler(value, row, column) {
        const property = column['property'];
        return row[property] === value;
      },
      getTableHeaderLabel(str){
          return this.tableHeadeList[str];
      }
    },
  }
</script>
